/*Test Cases Created by Steven Chow
 * This Test Cases tests for all the methods 
 * with in the Administrator Database
 * 
 * All Tests are independant from each other.
 */
package com.JUnitTestsDB;

import java.sql.*;

import static org.junit.Assert.*;
import org.junit.Test;

import com.database.AdministratorDB;

public class AdministratorDBTest {

	private Connection connection = null;
	private int employeeID;
	private String password;
	private String firstName;
	private String lastName;
	private String email;
	private int phone;

	// Setup Connection for Test
	public AdministratorDBTest() {
		String connectionString = "jdbc:mysql://localhost/planesimple?" // CHANGE
																		// THIS
																		// PART!!!
				+ "user=local&password=bluebeariscute89";
		try {
			// Ensure the SQL Server driver class is available.
			Class.forName("com.mysql.jdbc.Driver");

			// Establish the connection.
			connection = DriverManager.getConnection(connectionString);
		}
		// Exception handling
		catch (ClassNotFoundException cnfe) {

			System.out.println("ClassNotFoundException " + cnfe.getMessage());
		} catch (Exception e) {
			System.out.println("Exception " + e.getMessage());
			e.printStackTrace();
		}
	}

	// For Testing Modify methods
	public String GetAttributeFromDB(int columnNum, int tupleID)
			throws SQLException {
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + "Administrator"
				+ " where employeeID = " + "'" + tupleID + "'";
		ResultSet rs = stmt.executeQuery(sqlString);
		if (rs.next()) {
			this.employeeID = Integer.parseInt(rs.getString("employeeID"));
			switch (columnNum) {
			case 1:
				this.password = rs.getString("password");
				return this.password;
			case 2:
				this.firstName = rs.getString("firstname");
				return this.firstName;
			case 3:
				this.lastName = rs.getString("lastname");
				return this.lastName;
			case 4:
				this.email = rs.getString("email");
				return this.email;
			}
		}

		// tuple or Column Number Does not exist
		return null;
	}

	// For Testing Modify methods
	public int getPhoneFromDB(int tupleID) throws SQLException {
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + "Administrator"
				+ " where employeeID = " + tupleID;

		ResultSet rs = stmt.executeQuery(sqlString);

		if (rs.next()) {
			this.employeeID = Integer.parseInt(rs.getString("employeeID"));
			this.phone = Integer.parseInt(rs.getString("phone"));
			return this.phone;
		} else {
			return 0;
		}
	}

	// Test 1 Inserting 1 Admin to the DB
	@Test
	public void testInsert() {
		AdministratorDB db = new AdministratorDB(2, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// Always delete the existing data before starting the test
			// db.Delete("2");
			db.Insert();
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(true, db.UserExists(2));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// Test 2 Deleting 1 Admin from the DB
	@Test
	public void testDelete() {
		AdministratorDB db = new AdministratorDB(100, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// Always delete the existing data before starting the test
			// db.Delete("100");
			db.Insert();
			db.Delete("100");
			assertEquals(false, db.UserExists(100));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 3 for an Invalid User in the Database
	@Test
	public void testUserDoesNotExist() {
		AdministratorDB db = new AdministratorDB();
		db.Setup(connection);
		try {
			assertEquals(false, db.UserExists(9999));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 4 for a null password- DON'T NEED ANYMORE
	@Test
	public void testInsertNullPassword() {
		AdministratorDB db = new AdministratorDB(3, null, "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// Always delete the existing data before starting the test
			// db.Delete("3");
			db.Insert();
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(false, db.UserExists(3));
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				assertEquals(false, db.UserExists(3));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 5 Insert a Negative Phone Number -- DON'T NEED
	@Test
	public void testInsertNegativePhoneNumber() {
		AdministratorDB db = new AdministratorDB(4, "pass", "bob", "douche",
				"coolguy", -778);
		db.Setup(connection);
		try {
			// Always delete the existing data before starting the test
			// db.Delete("4");
			db.Insert();
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(false, db.UserExists(4));
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				assertEquals(false, db.UserExists(4));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 6 if you can modify the Admin's Password
	@Test
	public void testModPassword() {
		AdministratorDB db = new AdministratorDB(5, "pass", "bob", "douche",
				"coolguy", 778);

		db.Setup(connection);
		try {
			// db.Delete("5");
			db.Insert();
			db.ModPassword("pass1", 5);
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(true, db.UserExists(5));
			assertEquals("pass1", GetAttributeFromDB(1, 5));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 7 if you can modify the Admin's First Name
	@Test
	public void testModFirstName() {
		AdministratorDB db = new AdministratorDB(6, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("6");
			db.Insert();
			db.ModFirstName("steven", 6);
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals("steven", GetAttributeFromDB(2, 6));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 8 if you can modify the Admin's Last Name
	@Test
	public void testModLastName() {
		AdministratorDB db = new AdministratorDB(7, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("7");
			db.Insert();
			db.ModLastName("hi", 7);
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals("hi", GetAttributeFromDB(3, 7));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 9 if you can modify the Admin's E-mail
	@Test
	public void testModEmail() {
		AdministratorDB db = new AdministratorDB(8, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("8");
			db.Insert();
			db.ModEmail("bob@gmail.com", 8);
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals("bob@gmail.com", GetAttributeFromDB(4, 8));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 10 if you can modify the Admin's Phone Number
	@Test
	public void testModPhone() {
		AdministratorDB db = new AdministratorDB(9, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("9");
			db.Insert();
			db.ModPhone(1234, 9);
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(1234, getPhoneFromDB(9));
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
		}
	}

	// Test 11 for mod negative Phone numbers- Don't need
	@Test
	public void TestModNegativePhoneNumber() {
		AdministratorDB db = new AdministratorDB(10, "pass", "bob", "douche",
				"coolguy", -778);
		db.Setup(connection);
		try {
			// db.Delete("10");
			db.Insert();
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(778, getPhoneFromDB(9));
			// db.Delete("10");
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals(778, getPhoneFromDB(9));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 12 for mod null passwords
	@Test
	public void TestModNullPassword() {
		AdministratorDB db = new AdministratorDB(11, "goingtobenull", "bob",
				"douche", "coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("11");
			db.Insert();
			// Check to see if Test is passed
			// 1st Parameter is the "EXPECTED RESULT" & 2nd Parameter is
			// "ACTUAL RESULTS"
			// If the 2 match, then the test passed.
			assertEquals(true, db.UserExists(11));
			db.ModPassword(null, 11);
			assertEquals("goingtobenull", GetAttributeFromDB(1, 11));
			// db.Delete("10");
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals("goingtobenull", GetAttributeFromDB(1, 11));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 13 for Invalid password Length
	@Test
	public void testInvaildLengthForPassword() {
		AdministratorDB db = new AdministratorDB(12, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("12");
			db.Insert();
			assertEquals(true, db.UserExists(12));
			db.ModPassword("1234567890fgfgfgfgfgfgfgfgfg12345678901", 12);
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals("pass", GetAttributeFromDB(1, 12));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 14 for Invalid 1st Name Length
	@Test
	public void testInvaildLengthForFirstName() {
		AdministratorDB db = new AdministratorDB(13, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("13");
			db.Insert();
			assertEquals(true, db.UserExists(13));
			db.ModFirstName("123456789012345678901", 13);
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals("bob", GetAttributeFromDB(2, 13));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 15 for Invalid Last Name Length
	@Test
	public void testInvaildLengthForLastName() {
		AdministratorDB db = new AdministratorDB(14, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("14");
			db.Insert();
			assertEquals(true, db.UserExists(14));
			db.ModLastName("123456789012345678901", 14);
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals("douche", GetAttributeFromDB(3, 14));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	// Test 16 for Invalid Email Length
	@Test
	public void testInvaildLengthForEmail() {
		AdministratorDB db = new AdministratorDB(15, "pass", "bob", "douche",
				"coolguy", 778);
		db.Setup(connection);
		try {
			// db.Delete("15");
			db.Insert();
			assertEquals(true, db.UserExists(15));
			db.ModEmail("123456789012345674444545454454454sfgdfsgfg8901", 15);
		} catch (SQLException e) {
			System.out.println("EXCEPTION BABY!!");
			try {
				assertEquals("coolguy", GetAttributeFromDB(4, 15));
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

}